<?php

class Online_model extends Model {
	
	private $table 	= 'online';
	
	function Online_model() {
		parent::Model ();
	}
	
	function online_visit() {
		
		$time 		= time();	
		$timeout 	= 60*30;	
		$session_id = $this->session->userdata('session_id');;
		$ip_address = $this->input->ip_address();
		
		$query = $this->db->query("SELECT `sid` FROM `$this->table` WHERE `sid` = '$session_id'");
		
		$num = $query->num_rows();
	
		if ( $num == 0 ) 
			$this->db->query("INSERT INTO `$this->table` ( `timestamp`, `sid`, `ip` ) VALUES ( '$time', '$session_id', '$ip_address')");
		else 
			$this->db->query("UPDATE `$this->table` SET `timestamp` ='$time', `ip` = '$ip_address' WHERE `sid` = '$session_id' ");

		$time_exit = $time - $timeout;	
		
		$this->db->query("DELETE FROM `$this->table` WHERE `timestamp` < '$time_exit' ");
		
		$query = $this->db->query("SELECT DISTINCT `sid` FROM `$this->table` WHERE `sid` != '' ");
		
		$total_online = $query->num_rows();
		
		return $total_online;
	}
	
	
}
?>